﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Exam.Core.DBUtility;

namespace Exam.Core.DAL {
    public class StudentPaper {
        public List<Exam.Core.Model.StudentPaper> getStudentPaperByStuId(int stuId) {
            //string SQL = "SELECT * FROM Paper WHERE PaperID IN (SELECT PaperID FROM StudentPaper WHERE StudentID=1 AND PaperStatus<2)";
            string sql = "SELECT * FROM StudentExamPaper WHERE StudentID=" + stuId + " AND PaperStatus<2";
            DataTable dt = DbHelperOleDb.Query(sql).Tables[0];
            List<Exam.Core.Model.StudentPaper> list = new List<Exam.Core.Model.StudentPaper>();
            foreach(DataRow dr in dt.Rows){
                Exam.Core.Model.StudentPaper obj = new Exam.Core.Model.StudentPaper();
                obj.PaperID = dr["PaperID"].ToString().Trim();
                obj.PaperName = dr["PaperName"].ToString().Trim();
                obj.PaperDesc = dr["PaperDesc"].ToString().Trim();
                obj.PaperTime = dr["PaperTime"].ToString().Trim();
                obj.CreatTime = dr["CreatTime"].ToString().Trim();
                obj.StarTime = dr["StarTime"].ToString().Trim();
                obj.EndTime = dr["EndTime"].ToString().Trim();
                obj.PaperContent = dr["PaperContent"].ToString().Trim();
                obj.PaperType = dr["PaperType"].ToString().Trim();
                obj.CourseId = dr["CourseId"].ToString().Trim();
                obj.DepartmentID = dr["DepartmentID"].ToString().Trim();
                //obj.PaperQuestionTypes = getQuestionType(obj.PaperContent.Trim());
                obj.PaperStatus = dr["PaperStatus"].ToString().Trim();
                list.Add(obj);
            }
            return list;
        }

        public int getSize(string stuId, string starTime, string endTime, string keyword) {
            StringBuilder sb = new StringBuilder();
            sb.Append(" WHERE 1=1 ");
            if (stuId.Trim() != "")
                sb.Append(" AND StudentID=" + stuId + "");
            if (starTime.Trim() != "")
                sb.Append(" AND CreatTime >= '" + starTime + "'");
            if (endTime.Trim() != "")
                sb.Append(" AND CreatTime <= '" + endTime + "'");
            if (keyword.Trim() != "")
                sb.Append(" AND (PaperName like '%" + keyword.Trim() + "%' OR PaperDesc like '%" + keyword.Trim() + "%')");

            sb.Append(" AND PaperStatus<2 ");
            StringBuilder SQL = new StringBuilder();
            SQL.Append("SELECT Count(*)  FROM StudentExamPaper " + sb.ToString());

            object obj = DbHelperOleDb.GetSingle(SQL.ToString());
            return int.Parse(obj.ToString());
        }

        public List<Exam.Core.Model.StudentPaper> getPaperListByPage(string stuId, int pagesize, int page, string starTime, string endTime, string keyword) {
            StringBuilder sb = new StringBuilder();
            sb.Append(" WHERE 1=1 ");
            if (stuId.Trim() != "")
                sb.Append(" AND StudentID=" + stuId + "");
            if (starTime.Trim() != "")
                sb.Append(" AND CreatTime >= '" + starTime + "'");
            if (endTime.Trim() != "")
                sb.Append(" AND CreatTime <= '" + endTime + "'");
            if (keyword.Trim() != "")
                sb.Append(" and (PaperName like '%" + keyword.Trim() + "%' OR PaperDesc like '%" + keyword.Trim() + "%')");

            sb.Append(" AND PaperStatus<2 ");

            StringBuilder SQL = new StringBuilder();
            SQL.Append("SELECT TOP " + pagesize + "  *  FROM StudentExamPaper ");
            if (page <= 0) {
                SQL.Append(sb.ToString());
            }
            else {
                SQL.Append(sb.ToString());
                SQL.Append(" AND PaperID NOT IN ( ");
                SQL.Append(" SELECT TOP " + pagesize * page + " PaperID FROM StudentExamPaper ");
                SQL.Append(sb.ToString() + " ) ");

            }
            sb.Append(" order by PaperID desc");

            DataTable dt = DbHelperOleDb.Query(SQL.ToString()).Tables[0];
            List<Exam.Core.Model.StudentPaper> list = new List<Exam.Core.Model.StudentPaper>();
            foreach (DataRow dr in dt.Rows) {
                Exam.Core.Model.StudentPaper obj = new Exam.Core.Model.StudentPaper();
                obj.PaperID = dr["PaperID"].ToString().Trim();
                obj.PaperName = dr["PaperName"].ToString().Trim();
                obj.PaperDesc = dr["PaperDesc"].ToString().Trim();
                obj.PaperTime = dr["PaperTime"].ToString().Trim();
                obj.CreatTime = dr["CreatTime"].ToString().Trim();
                obj.StarTime = dr["StarTime"].ToString().Trim();
                obj.EndTime = dr["EndTime"].ToString().Trim();
                obj.PaperContent = dr["PaperContent"].ToString().Trim();
                obj.PaperType = dr["PaperType"].ToString().Trim();
                obj.CourseId = dr["CourseId"].ToString().Trim();
                obj.DepartmentID = dr["DepartmentID"].ToString().Trim();
                //obj.PaperQuestionTypes = getQuestionType(obj.PaperContent.Trim());
                obj.PaperStatus = dr["PaperStatus"].ToString().Trim();
                list.Add(obj);
            }
            return list;
        }
        

    
    
    }
}
